package com.fudan.gamestore.service.impl;

import com.fudan.gamestore.mapper.CartItemMapper;
import com.fudan.gamestore.pojo.CartItem;
import com.fudan.gamestore.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private CartItemMapper cartItemMapper;

    @Override
    public List<CartItem> getCartByUserId(Integer userId) {
        return cartItemMapper.getCartByUserId(userId);
    }

    @Override
    public Integer addGameToCart(Integer userId, Integer gameId) {
        return cartItemMapper.addGameToCart(userId, gameId);
    }

    @Override
    public Integer checkIfInCart(Integer userId, Integer gameId) {
        return (cartItemMapper.getCartItem(userId, gameId) != null) ? 1 : 0;
    }

    @Override
    public Integer clearCart(Integer userId) {
        return cartItemMapper.clearCart(userId);
    }

    @Override
    public Integer deleteCartItems(Integer userId, List<Integer> gameIdList) {
        return cartItemMapper.deleteCartItems(userId, gameIdList);
    }

    @Override
    public Integer deleteCartItems(List<Integer> cartItemIds) {
        for (Integer cartItemId : cartItemIds)
            cartItemMapper.deleteCartItem(cartItemId);
        return 1;
    }
}
